﻿using System;
using System.Collections.Generic;
using System.Text;

using Obsidian.Utils;
using Obsidian.Action;
using Obsidian.Web;
using Obsidian.Edm;
using Obsidian.Runtime;
using Oak.Base;
using Oak.Model;
using Oak.MagSys.Data;

namespace Oak.MagSys.Logic
{
    public class AdminIconLG : OakLG<ISession>
    {

        public AdminIconLG(ISession session) : base(session) { }

        public bool UpdateAdminIconsByAdminId(long adminId, IEnumerable<long> iconIds, out string msg)
        {
            if (!Session.IsSuperAdmin)
            {
                msg = "当前账号没有权限执行该操作";
                return false;
            }

            List<MsAdminIconInfo> list = new List<MsAdminIconInfo>();
            foreach (long iconId in iconIds)
            {
                MsAdminIconInfo icon = new MsAdminIconInfo();
                icon.AdminId.Set(adminId);
                icon.IconId.Set(iconId);
                icon.Created.Now();
                list.Add(icon);
            }

            AdminIconDA iconDA = new AdminIconDA();
            try
            {
                iconDA.UpdteAdminIconsByAdminId(adminId, list);
            }
            catch (Exception ex)
            {
                string code = Logger.Error(ex);
                msg = "发生系统错误:" + code;
                return false;
            }

            msg = "";
            return true;

        }


        public List<MsAdminIconInfo> GetAdminIconList(MsAdminIconQry qry, out string msg)
        {
            List<MsAdminIconInfo> list;

            try
            {
                AdminIconDA iconDA = new AdminIconDA();
                list = iconDA.SelectList(qry);
            }
            catch (Exception ex)
            {
                string code = Logger.Error(ex);
                msg = "发生系统错误:" + code;
                return null;
            }

            msg = "";
            return list;

        }

        public List<MsIconInfo> ListingAdminIcons(out string msg)
        {
            if (!Session.IsAdmin)
            {
                msg = "当前账号没有权限执行该操作";
                return null;
            }

            long adminId = Session.AccountId;

            List<MsIconInfo> list;
            try
            {
                if (Session.IsSuperAdmin)
                {
                    IconDA iconDA = new IconDA();
                    list = iconDA.SelectList(new MsIconQry());
                }
                else
                {
                    AdminIconDA iconDA = new AdminIconDA();
                    MsAdminIconQry qry = new MsAdminIconQry();
                    qry.AdminId.Set(adminId);
                    qry.IsSystemIcon.Set(false);
                    List<MsAdminIconInfo> items = iconDA.SelectList(qry);

                    list = new List<MsIconInfo>(items.Count);
                    foreach (MsAdminIconInfo item in items)
                        list.Add(item.Icon.Value);
                }
            }
            catch (Exception ex)
            {
                string code = Logger.Error(ex);
                msg = "发生系统错误:" + code;
                return null;
            }

            msg = "";
            return list;
        }

    }
}
